<?php

namespace app\admin\controller;

use app\admin\library\Auth;
use app\common\controller\Backend;
use think\Config;
use Think\Db;
use think\Session;
/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{
    /**
     * 查看
     */
    public function index()
    {
        $admin_data = Session::get("admin");
        //查询当前用户权限
        $auth = new \app\admin\library\Auth;
        $group =  $auth->getGroups($admin_data['id']);//获取当前用户权限
        $seventtime = \fast\Date::unixtime('day', -7);
        $paylist = $createlist = [];
        for ($i = 0; $i < 7; $i++)
        {
            $day = date("Y-m-d", $seventtime + ($i * 86400));
            $createlist[$day] = mt_rand(20, 200);
            $paylist[$day] = mt_rand(1, mt_rand(1, $createlist[$day]));
        }
        $hooks = config('addons.hooks');
        $uploadmode = isset($hooks['upload_config_init']) && $hooks['upload_config_init'] ? implode(',', $hooks['upload_config_init']) : 'local';
        $addonComposerCfg = ROOT_PATH . '/vendor/karsonzhang/fastadmin-addons/composer.json';
        Config::parse($addonComposerCfg, "json", "composer");
        $config = Config::get("composer");
        $addonVersion = isset($config['version']) ? $config['version'] : __('Unknown');
        //查询总客户人数
        $linkBuser = Db::name('Buser');
        $buserCount = $linkBuser->count();
        //查询签约客户数
        $linkAllot = Db::name('allot');
        $qianUser = $linkAllot->where(array('status'=>6))->group('buser_id')->column('buser_id');
        $qianCount = count($qianUser);
        //查询合作中客户
        $heCount = $linkAllot->where(array('typelist'=>2))->group('buser_id')->count();
        //查询没有签约的客户
        $tingCount = $linkAllot->where(array('buser_id'=>array('NOT IN',$qianUser)))->group('buser_id')->count();
        //查询没有完成的任务
        $linkAdminwork = Db::name('adminwork');
        $notWorkCount = $linkAdminwork->where(array('admin_id'=>$admin_data['id'],'statuslist'=>array('NOT IN','4')))->count();
        //查询完成的任务数
        $wanWorkCount = $linkAdminwork->where(array('admin_id'=>$admin_data['id'],'statuslist'=>4))->count();
        //查询所有任务
        $zongWorkCount = $linkAdminwork->where(array('admin_id'=>$admin_data['id']))->count();
        //查询待验收的任务
        $yanWorkCount = $linkAdminwork->where(array('admin_id'=>$admin_data['id'],'statuslist'=>2))->count();
        //查询被驳回的任务
        $boWorkCount = $linkAdminwork->where(array('admin_id'=>$admin_data['id'],'statuslist'=>3))->count();
        //查询已经逾期期的任务
        $qiWorkCount = $linkAdminwork->where(array('admin_id'=>$admin_data['id'],'qilist'=>2))->count();
        //查询图形数据
        $linkCooperation = Db::name('cooperation');
     /*   $monthTime =  strtotime(date('Y-m',time()));*/
     /*   $outTime = strtotime('+6 day',$monthTime)+86400;

        $cooperationData = $linkCooperation->field('SUM(much) AS much,FROM_UNIXTIME(createtime,"%Y年%m月%d日") AS time')
                            ->where(array('createtime'=>array(array('EGT',$monthTime),array('ELT',$outTime))))
                            ->group('FROM_UNIXTIME(createtime,"%Y年%m月%d日") ')

                            ->select();
        $dataArray = array('time'=>array(),'money'=>array());
        foreach($cooperationData as $k=>$v){
            $dataArray['time'][] = $v['time'];
            $dataArray['money'][] = $v['much'];
        }*/
        //查询本月首签
        $cooperationWhere = array('typelist'=>1);
        $monthArray = array();
        if($group[0]['group_id']!=1&&$group[0]['group_id']!=2){
            //非管理人员，查询本人业绩
            $cooperationWhere['admin_id'] = $admin_data['id'];
        }
        $monthData = $linkCooperation->field('SUM(much) AS much')
                    ->whereTime('createtime','>=',strtotime(date('Y-m',time())))
                     ->where($cooperationWhere)->select();
        $monthArray['benMonth'][] = isset($monthData[0]['much'])?$monthData[0]['much']/10000:0.00;
        //查询本月续签
        $cooperationWhere['typelist'] = 2;
        $monthData = $linkCooperation->field('SUM(much) AS much,typelist')
            ->whereTime('createtime','>=',strtotime(date('Y-m',time())))
            ->where($cooperationWhere)->select();
        $monthArray['benMonth'][] = isset($monthData[0]['much'])?$monthData[0]['much']/10000:0.00;
        //查询上月首签数据
        $qiTime =  date('Y-m-01', strtotime('-1 month'));
        $endTime =  date('Y-m-t', strtotime('-1 month'));
        $cooperationWhere['typelist'] = 1;
        $monthData = $linkCooperation->field('SUM(much) AS much,typelist')
            ->whereTime('createtime','BETWEEN',[$qiTime,$endTime])
            ->where($cooperationWhere)->select();
        $monthArray['upMonth'][] = isset($monthData[0]['much'])?$monthData[0]['much']/10000:0.00;
        //查询上月续签
        $cooperationWhere['typelist'] = 2;
        $monthData = $linkCooperation->field('SUM(much) AS much,typelist')
            ->whereTime('createtime','BETWEEN',[$qiTime,$endTime])
            ->where($cooperationWhere)->select();
        $monthArray['upMonth'][] = isset($monthData[0]['much'])?$monthData[0]['much']/10000:0.00;
        //查询半年首签数据
        $qiTime =  strtotime('-6 month');
        $cooperationWhere['typelist'] = 1;
        $monthData = $linkCooperation->field('SUM(much) AS much,typelist')
            ->whereTime('createtime','>=',$qiTime)
            ->where($cooperationWhere)->select();
        $monthArray['ban'][] = isset($monthData[0]['much'])?$monthData[0]['much']/10000:0.00;
        //查询半年续签数据
        $cooperationWhere['typelist'] = 2;
        $monthData = $linkCooperation->field('SUM(much) AS much,typelist')
            ->whereTime('createtime','>=',$qiTime)
            ->where($cooperationWhere)->select();
        $monthArray['ban'][] = isset($monthData[0]['much'])?$monthData[0]['much']/10000:0.00;
        //查询一年的首签数据
        $qiTime =  strtotime('-12 month');
        $cooperationWhere['typelist'] = 1;
        $monthData = $linkCooperation->field('SUM(much) AS much,typelist')
            ->whereTime('createtime','>=',$qiTime)
            ->where($cooperationWhere)->select();
        $monthArray['year'][] = isset($monthData[0]['much'])?$monthData[0]['much']/10000:0.00;
        //查询一年的续签数据
        $qiTime =  strtotime('-12 month');
        $cooperationWhere['typelist'] = 2;
        $monthData = $linkCooperation->field('SUM(much) AS much,typelist')
            ->whereTime('createtime','>=',$qiTime)
            ->where($cooperationWhere)->select();
        $monthArray['year'][] = isset($monthData[0]['much'])?$monthData[0]['much']/10000:0.00;
        //查询第一条成交记录的时间
        $chengData = $linkCooperation->order('id')->find();
        $day = ($chengData)?floor((time()-$chengData['createtime'])/86400):0;
        $this->view->assign('day',$day);
        $this->view->assign([
            'totaluser'        => $buserCount,
            'totalviews'       => $qianCount,
            'totalorder'       => $heCount,
            'totalorderamount' => $tingCount,
            'todayuserlogin'   => $notWorkCount,
            'todayusersignup'  => $zongWorkCount,
            'todayorder'       => $wanWorkCount,
            'unsettleorder'    => $yanWorkCount,
            'sevendnu'         => $boWorkCount,
            'sevendau'         => $qiWorkCount,
            'paylist'          => $paylist,
            'createlist'       => $createlist,
            'addonversion'       => $addonVersion,
            'uploadmode'       => $uploadmode,
            'monthArray'       =>$monthArray
        ]);
        return $this->view->fetch();
    }

    /**
     * ajax查询eachet数据
     */
    public function ajax_eachert(){
        $day = $this->request->post('day');
        $linkCooperation = Db::name('Cooperation');
        $qiTime = strtotime(date('Y-m-d 00:00:00',strtotime( -$day.' day')));
        $outTime = $qiTime+86399;
        $where['createtime'] = array(array('EGT',$qiTime),array('ELT',$outTime));
        $data = $linkCooperation->field('SUM(much) AS much,FROM_UNIXTIME(createtime,"%Y年%m月%d日") AS time')
                ->where($where)->select();
        $dataArray = array();
        if(!$data){
            $dataArray[0]['time'] = date('Y-m-d',$qiTime);
            $dataArray[0]['much'] = 0;
        }else{
            $dataArray = $data;
        }
        return json(array('code'=>1,'data'=>$dataArray));
    }
}
